;
define(["MComponent", "MImage", "MList", "MOTimer", "MLLayer", "MPanel"], function(MComponent, MImage, MList, MOTimer, MLLayer, MPanel) {
	/**
	 * 面板
	 * @param {Object} background 背景
	 */
	var MCarousel = function(background) {
		this.dom = this.query("<div></div>", "MCarousel");
		this.setBackground(background);

		//组件
		var components = [];

		//当前播放
		var currentIndex = 0;

		//方向
		var direction = "TOP";

		//懒加载
		var lazyLoad = true;

		//动画延迟
		var animateUnit = 1000;

		//列表
		var list = new MList();

		//定时器
		var timer = new MOTimer();

		//显示方式
		var display = "VERTICAL";

		//层布局
		var layer = new MLLayer();

		//按钮
		var buttons = [];

		//块按钮
		var blockButtons = [];
		
		//块按钮的容器
		var blockButtonsContainer = new MPanel();
		
		var isStart = false;
		
		var isStatic = false;
		
		this.indicator = "BOTTOM";

		/**
		 * 更新组件
		 */
		this.updComponents = function(func) {
			if(func && typeof func == "function") {
				for(var i in components) {
					func(components[i]);
				}
			}
			return this;
		}

		/**
		 * 设置轮播间隔时间
		 */
		this.setTimeUnit = function(unit) {
			if(!unit) {
				unit = 5000;
			}
			timer.setUnit(unit);
			return this;
		}

		/**
		 * 设置背景
		 */
		this.setBackground = function(value) {
			layer.get(0).setBackground(value);
			return this;
		}

		/**
		 * 获得轮播组件
		 */
		this.getComponent = function(index) {
			return components[index];
		}
		
		/**
		 * 获得指示器按钮
		 */
		this.getBlockButton = function(index){
			return blockButtons[index];
		};

		/**
		 * 放入组件
		 */
		this.putComponent = function(index, comp) {
			if(comp instanceof MImage) {

				components[index] = comp;

				if(!lazyLoad || index == 0) {
					comp.setDefaultSrc(comp.getSrc());
				}
				comp.setSize(this.getWidth(), this.getHeight());

				list.put(comp);

			} else if(comp instanceof MComponent) {

				components[index] = comp;
				comp.setSize(this.getWidth(), this.getHeight());
				list.put(comp);
			} else {
				var img = new MImage();
				img.setSize(this.getWidth(), this.getHeight());
				img.setSrc(comp);
				if(!lazyLoad || index == 0) {
					img.setDefaultSrc(img.getSrc());
				}
				components[index] = img;
				list.put(img);
			}

			this.addBlockButton();

			return this;
		}

		this.addBlockButton = function() {
			var button = new MPanel().setSize(10, 10);
			button.onSelect = function(){
				button.setBackground("#fff").setOpaque(100);
			}
			
			button.onDefault = function(){
				button.setBackground("#fff").setOpaque(50);
			}
			
			button.mindex = blockButtons.length;
			
			var that = this;
			
			button.handleHover(function(){
				
				if(!isStatic){
					that.stop();
				}
			},function(){
				if(!isStatic){
					that.start();
				}
			});
			
			button.handle("click",function(){
				currentIndex = button.mindex-1;
				that.render(display,null,that.indicator);
			});
			blockButtons.push(button);
			blockButtonsContainer.append(button);
		}
		
		/**
		 * 重置
		 */
		this.reset = function(){
			this.updComponents(function(comp){
				comp.remove();
			});
			components = null;
			components = [];
			this.updateBlockButtons(function(comp){
				comp.remove();
			});
			blockButtons = null;
			blockButtons = [];
		}

		/**
		 * 设置组件
		 */
		this.setComponents = function(array) {
			this.reset();
			if(array instanceof Array) {

				for(var comp in array) {
					this.putComponent(comp, array[comp]);
				}

			} else {
				this.putComponent(0, array);
			}
			return this;
		};

		//下一张
		this.next = function() {
			currentIndex++;
			currentIndex = this.vertify(currentIndex);
			this.render(display,true,this.indicator);
			return this;
		};

		//前一张
		this.prev = function() {
			currentIndex--;
			currentIndex = this.vertify(currentIndex);
			this.render(display,true,this.indicator);
			return this;
		};

		/**
		 * 验证边界
		 */
		this.vertify = function(index) {
			if(index < 0) {
				index = this.size() - 1;
			}

			if(index > this.size() - 1) {
				index = 0;
			}

			return index;
		}

		/**
		 * 渲染模式
		 */
		this.render = function(display,flag,value) {
			this.boxResize(display);
			this.scrollToPosition(display,flag,value);
			var img = components[currentIndex];
			if(lazyLoad && img instanceof MImage) {
				img.loadImage();
			}
			var btn = blockButtons[currentIndex];
			this.updateBlockButtons(function(comp){
				if(comp.onDefault){
					comp.onDefault();
				}
			});
			if(btn && btn.onSelect){
				btn.onSelect();
			}
		}
		
		this.boxResize = function(display) {
			var size = blockButtons.length;
			switch(display) {
				case "HORIZONTAL":
					blockButtonsContainer.setSize( size * 20, 20);
					for(var index in blockButtons){
						var button = blockButtons[index];
						button.setMargin(5,5,5,5);
						button.setStyle("display", "inline-block");
						var ie6 = function() {
							button.setStyle("display", "inline");
							button.setStyle("zoom", "1");
						}
						blockButtonsContainer.setStyle("left","50%");
						blockButtonsContainer.setMargin(- size * 10,null,null,null);
						mofum.ieCallback(ie6, ie6, null, null, ie6);
					}
					break;
				case "VERTICAL":
					for(var index in blockButtons){
						var button = blockButtons[index];
						if(index == 0){
							button.setMargin(5,5,5,10);
						}else{
							button.setMargin(5,5,0,10);
						}
						button.setStyle("display", "block");
					}
					blockButtonsContainer.setSize(20, size * 20);
					blockButtonsContainer.setStyle("top","50%");
					blockButtonsContainer.setMargin(null,null,- size * 10,null);
					break;

			}
			return this;
		}

		/**
		 * 滚动到指定位置
		 */
		this.scrollToPosition = function(display,animation,value) {
			
			var width = this.getWidth();

			var height = this.getHeight();
			
			switch(display) {
				case "HORIZONTAL":
					if(animation){
						this.scroll(0, currentIndex * width);
					}else{
						this.noAnimationScroll(0, currentIndex * width);
					}
					
					if(value){
						var height = this.getHeight() - blockButtonsContainer.getHeight();
						if(value  ==  "TOP"){
							blockButtonsContainer.setLocation(null,null,0,null);
						}else{
							blockButtonsContainer.setLocation(null,null,height,null);
						}
					}
					
					break;
				case "VERTICAL":
					if(animation){
						this.scroll(currentIndex * height, 0);
					}else{
						this.noAnimationScroll(currentIndex * height, 0);
					}
					if(value){
						var width = this.getWidth() - blockButtonsContainer.getWidth();
						if(value  ==  "LEFT"){
							blockButtonsContainer.setLocation(0,null,null,null);
						}else{
							blockButtonsContainer.setLocation(width,null,null,null);
						}
					}
					break;

			}
			return this;
		};
		
		this.noAnimationScroll = function(top, left){
			list.dom.scrollLeft(left);
			list.dom.scrollTop(top);
		}

		/**
		 * 滚动
		 */
		this.scroll = function(top, left) {
			list.dom.animate({
				scrollTop: top,
				scrollLeft: left
			}, animateUnit, function() {
				isAnimate = false;
			});
		}

		/**
		 * 设置图片懒加载
		 */
		this.setLazyLoad = function(flag) {
			lazyLoad = flag;
			this.updComponents(function(comp) {
				if(comp instanceof MImage) {
					if(!lazyLoad) {
						comp.setDefaultSrc(comp.getSrc());
					}

				}
			});
			return this;
		}

		/**
		 * 当前组件索引
		 */
		this.getCurrentIndex = function() {
			return currentIndex;
		};
		
		this.updateBlockButtons = function(func){
			if(func && typeof func == 'function'){
				for(var i in blockButtons){
					func(blockButtons[i]);
				}
			}
		}

		/**
		 * 组件数量
		 */
		this.size = function() {
			return components.length;
		};

		/**
		 * 获得定时器
		 */
		this.getTimer = function() {
			return timer;
		}

		/**
		 * 设置高度
		 */
		this.setHeight = function(value) {
			var val = parseInt(value);
			height = val;
			this.setStyle("height", val + "px");
			this.updComponents(function(comp) {
				comp.setHeight(height);
			});
			list.setHeight(height);
			currentIndex = 0;
			list.dom.stop(true,true);
			this.render(display,isStatic,this.indicator);
			return this;
		};

		/**
		 * 设置宽度
		 */
		this.setWidth = function(value) {
			var val = parseInt(value);
			width = val;
			this.setStyle("width", val + "px");
			this.updComponents(function(comp) {
				comp.setWidth(width);
			});
			list.setWidth(width);
			currentIndex = 0;
			list.dom.stop(true,true);
			this.render(display,isStatic,this.indicator);
			return this;
		};

		/**
		 * 开始轮播
		 */
		this.start = function() {
			if(!isStart){
				timer.start();
				isStart = true;
			}
			return this;
		}

		/**
		 * 停止
		 */
		this.stop = function() {
			if(isStart){
				timer.stop();
				isStart = false;
			}
			return this;
		}

		/**
		 * 暂停
		 */
		this.suspend = function() {
			if(isStart){
				timer.suspend();
				isStart = false;
			}
			return this;
		}

		/**
		 * 关闭垂直轮播
		 */
		this.closeVertical = function(flag) {
			if(flag) {
				display = "VERTICAL";
				list.setDisplay("VERTICAL");
				this.setIndicatorPosistion("RIGHT");
			} else {
				display = "HORIZONTAL";
				list.setDisplay("HORIZONTAL");
				this.setIndicatorPosistion("BOTTOM");
			}
			return this;
		}
		
		/**
		 * 渲染静态轮播
		 */
		this.renderStatic = function(){
			this.stop();
			isStatic = true;
			currentIndex = 0;
			list.dom.stop(true,true);
			this.render(display,true,this.indicator);
		};
		
		/**
		 * 设置指示器的位置
		 */
		this.setIndicatorPosistion = function(value){
			this.indicator = value;
			this.render(display,isStatic,this.indicator);
		};
		
		this.init();
		this.init = function() {
			layer.put(0); //背景层
			layer.put(1, list); //轮播层
			layer.put(2); //用户按钮层
			layer.get(2).append(blockButtonsContainer);
			this.append(layer);
			list.setScrollbarVisible(false);
			list.dom.scroll();
			list.setScrollflag(false);

			var that = this;
			timer.setTask(function(count) {
				that.next();
			});
			this.setTimeUnit(5000);
		}
		this.init();
	};
	mofum.extend(MCarousel, MComponent);
	return MCarousel;
});